package com.yanfan.energy.service.impl;


import com.yanfan.energy.domain.statist.CompanyBuildAreaEntity;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author huan
 * @Description
 * @ClassName 2023年08月15日 10:02:00
 * @Version 1.0
 **/
public class ExcelExporter {

    private static Map<Integer, String> STATUS_MAP = new HashMap<>();

    static {
        STATUS_MAP.put(1, "启用");
        STATUS_MAP.put(0, "停用");
    }

    public static void exportToExcel(List<CompanyBuildAreaEntity> entityList, HttpServletResponse response) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Company Build Areas");

        createHeaderRow(sheet);

        int[] rowIdx = {1};
        for (CompanyBuildAreaEntity entity : entityList) {
            writeEntityToRow(entity, sheet.createRow(rowIdx[0]++));
            writeChildrenToSheet(entity.getChildren(), entity.getName(), sheet, rowIdx);
        }

        String fileName = URLEncoder.encode("公司楼栋楼层区域列表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        workbook.write(response.getOutputStream());
    }

    private static void createHeaderRow(Sheet sheet) {
        Row headerRow = sheet.createRow(0);
        headerRow.createCell(0).setCellValue("主键");
        headerRow.createCell(1).setCellValue("名称");
        headerRow.createCell(2).setCellValue("标注");
        headerRow.createCell(3).setCellValue("负责人");
        headerRow.createCell(4).setCellValue("状态");
        headerRow.createCell(5).setCellValue("联系方式");
        headerRow.createCell(6).setCellValue("面积（㎡）");
    }

    private static void writeEntityToRow(CompanyBuildAreaEntity entity, Row row) {
        row.createCell(0).setCellValue(entity.getId());
        if (entity.getName() != null) {
            row.createCell(1).setCellValue(entity.getName());
        }
        if (entity.getType() != null) {
            row.createCell(2).setCellValue(entity.getTypeName());
        }
        if (entity.getPrincipalName() != null) {
            row.createCell(3).setCellValue(entity.getPrincipalName());
        }
        if (entity.getStatus() != null) {
            row.createCell(4).setCellValue(STATUS_MAP.get(entity.getStatus()));
        }
        if (entity.getPhone() != null) {
            row.createCell(5).setCellValue(entity.getPhone());
        }
        row.createCell(6).setCellValue(entity.getArea());
    }

    private static void writeChildrenToSheet(List<CompanyBuildAreaEntity> children, String parentName, Sheet sheet, int[] rowIdx) {
        if (children != null) {
            for (CompanyBuildAreaEntity child : children) {
                Row row = sheet.createRow(rowIdx[0]++);
                writeEntityToRow(child, row);
                writeChildrenToSheet(child.getChildren(), child.getName(), sheet, rowIdx);
            }
        }
    }
}
